//
//  BIDMemberDetailViewController.m
//  cxgov
//
//  Created by mal on 16/3/11.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDMemberDetailViewController.h"
#import <MessageUI/MessageUI.h>

//人员详情
static NSString *memberDetailURL = @"userAndroid!gainUserDetail.action";

@interface BIDMemberDetailViewController ()<MFMessageComposeViewControllerDelegate>

@end

@implementation BIDMemberDetailViewController
@synthesize memberId;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详细信息";
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:RGBACOLOR(226, 233, 243, 1)];
    _containerView1.clipsToBounds = YES;
    _containerView1.layer.cornerRadius = 3.f;
    
    _containerView2.clipsToBounds = YES;
    _containerView2.layer.cornerRadius = 3.f;
    
    _containerView3.clipsToBounds = YES;
    _containerView3.layer.cornerRadius = 3.f;
    
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], memberDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"userId=%@", memberId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(responseDic)
        {
            NSNumber *resValue = responseDic[@"isSuccess"];
            if(resValue && [resValue intValue]==1)
            {
                responseDic = responseDic[@"userDetail"];
                NSString *strName = responseDic[@"trueName"];
                NSString *strOfficePhone = responseDic[@"cphone"];
                NSString *strMobilePhone = responseDic[@"mphone"];
                NSString *strDepartmentName = responseDic[@"departmentName"];
                NSString *strPositionName = responseDic[@"postName"];
                //
                _nameLabel.text = strName;
                _officePhoneLabel.text = strOfficePhone;
                _mobilePhoneLabel.text = strMobilePhone;
                _departmentLabel.text = strDepartmentName;
                _positionLabel.text = strPositionName;
            }
        }
    }];
}

- (void)callWithNumber:(NSString*)phoneNumber
{
    NSString *strPhone = phoneNumber;
    NSString *str = [[NSString alloc] initWithFormat:@"tel://%@", strPhone];
    NSURL *url = [NSURL URLWithString:str];
    UIWebView *telWebView = [[UIWebView alloc] init];
    [telWebView loadRequest:[NSURLRequest requestWithURL:url]];
    [self.view addSubview:telWebView];
}
//拨打办公电话
- (IBAction)officeTelephoneHandler:(id)sender
{
    [self callWithNumber:_officePhoneLabel.text];
}
//拨打移动电话
- (IBAction)mobileTelephoneHandler:(id)sender
{
    [self callWithNumber:_mobilePhoneLabel.text];
}
//发短信
- (IBAction)sendMsgHandler:(id)sender
{
    NSString *strPhone = _mobilePhoneLabel.text;
    if([MFMessageComposeViewController canSendText])
    {
        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        NSArray *arr = [[NSArray alloc] initWithObjects:strPhone, nil];
        controller.recipients = arr;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:^{}];
    }
}

#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [controller dismissViewControllerAnimated:YES completion:^{}];
}

@end
